import dt from '@dt/dt'
import fitClassApi from '@/config/server/api/fitClass'
import buildingApi from '@/config/server/api/building'
import toolApi from '@/biz/base/tool'


function returnRender() {
  return (h, {
    root,
    node,
    data
  }) => {
    return h(
      'span', {
        style: {
          display: 'inline-block',
          width: '100%',
          color: data.level != 4 && data.childrenLen == 0 ? '#ccc' : '',
        },
      },
      [
        h('span', [
          h('span', data.title),
          h(
            'span',
            ` ${data.level=='2'?'(' + data.childrenLen + ')':''}`
          ),
        ]),
      ]
    )
  }
}

function returnRenderchildren() {
  return (h, {
    root,
    node,
    data
  }) => {
    return h(
      'span', {
        style: {
          display: 'inline-block',
          width: '100%',
          color: data.disabled ? '#ccc' : '',
        },
      },
      data.title
    )
  }
}
//循环遍历部门
function forEachDepartment(mess) {
  mess.map((item) => {
    item.title = item.orgName
    item.id = item.orgId
    item.childrenLen = item.buildingNumber ? item.buildingNumber : item.children.length
    item.children && forEachDepartment(item.children)
    item.render = returnRender()
  })
}

function flattenArr(arr) {
  let flattenArr = []
  arr.forEach((item) => {
    if (item.children) {
      item.children.forEach((childItem) => {
        if (childItem.level == '4') {
          flattenArr.push({
            title: childItem.orgName,
            id: childItem.orgId,
          })
        }
        if (childItem.children.length > 0) {
          childItem.children.forEach((childItemC) => {
            if (childItemC.level == '4') {
              flattenArr.push({
                title: childItemC.orgName,
                id: childItemC.orgId,
              })
            }
            if (childItemC.children.length > 0) {
              childItemC.children.forEach((childItemV) => {
                if (childItemV.level == '4') {
                  flattenArr.push({
                    title: childItemV.orgName,
                    id: childItemV.orgId,
                  })
                }
              })
            }
          })
        }
      })
    }
  })
  return flattenArr
}
//获取当前登录人下的组织/案场列表（未设置目标的置灰）
function getUserByRole(params) {
  return toolApi.getBuildingOrg(params).then(res => {
    let treeOptions = flattenArr(res)
    res.forEach((item) => {
      item.title = item.orgName
      item.id = item.orgId
      item.expand = true
      item.disabled = true
      // item.render = returnRenderchildren()
      item.children && forEachDepartment(item.children)
    })
    return {
      treeList: res,
      treeOptions
    }
  })
}
//根据案场获取该案场的执行周期列表
function getExecutionCycle(params) {
  return fitClassApi.getExecutionCycle.fetch(params).then(res => {
    console.log(res, 'getExecutionCycle')
    return res
  })
}
//累积汇总列表导出
function accruingListForExcel(params) {
  return fitClassApi.accruingListForExcel.fetch(params)
}
//累积汇总列表(根据执行周期的主键id)
function getAccruing(params) {
  return fitClassApi.getAccruing.fetch(params)

}
//
function reachAllInfo(params) {
  return fitClassApi.reachAllInfo.fetch(params)
}
export default {
  getUserByRole,
  getExecutionCycle,
  accruingListForExcel,
  getAccruing,
  reachAllInfo
}